<template>
  <el-form :model="form" ref="form" :rules="rules" class="form">
    <el-form-item class="form-item" prop="username">
      <el-input placeholder="用户名手机" v-model="form.username" @focus="handMsg('username')"></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="captcha">
      <el-input placeholder="验证码" v-model="form.captcha" @focus="handMsg('captcha')">
        <template slot="append">
          <el-button @click="handleSendCaptcha">发送验证码</el-button>
        </template>
      </el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="nickname">
      <el-input placeholder="你的名字" v-model="form.nickname" @focus="handMsg('nickname')"></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="password">
      <el-input
        placeholder="密码"
        type="password"
        v-model="form.password"
        @focus="handMsg('password')"
      ></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="checkPass">
      <el-input placeholder="确认密码" type="password" v-model="form.checkPass"></el-input>
    </el-form-item>

    <el-button class="submit" type="primary" @click="handleRegSubmit">注册</el-button>
  </el-form>
</template>

<script>
export default {
  data() {
    // 确认密码
    const validatePass = (rule, value, callback) => {
      if (value == this.form.password) {
        console.log("密码相同");
        callback();
      }
      if (value != this.form.password) {
        console.log("两次密码不相同");
        callback(new Error("两次密码必须相同"));
      }
    };
    return {
      // 表单数据
      form: {
        username: "",
        nickname: "",
        captcha: "",
        password: "",
        checkPass: ""
      },
      // 表单规则
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur"
          }
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur"
          }
        ],
        checkPass: [
          {
            validator: validatePass,
            trigger: "blur"
          }
        ],
        nickname: [
          {
            required: true,
            message: "请输入昵称",
            trigger: "blur"
          }
        ],
        captcha: [
          {
            required: true,
            message: "请输入验证码",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    handMsg(proName) {
      this.$refs.form.clearValidate(proName);
    },
    // 发送验证码
    handleSendCaptcha() {
      //   console.log(this.form);
      this.$axios({
        url: "/captchas",
        method: "POST",
        data: {
          tel: this.form.username
        }
      }).then(res => {
        console.log(res.data);
        this.$message.success("获取验证码成功");
      });
    },

    // 注册
    handleRegSubmit() {
      console.log(this.form);
      this.$refs.form.validate((isValid, itemNotValid) => {
        if (isValid) {
          console.log("注册成功");
          //   const RegisterData = {
          //     username: this.form.username,
          //     nickname: this.form.nickname,
          //     captcha: this.form.captcha,
          //     password: this.form.password
          //   };
          //剩余属性写法
          const { checkPass, ...RegisterData } = this.form;
          this.$axios({
            url: "/accounts/register",
            method: "POST",
            data: RegisterData
          }).then(res => {
            console.log(res.data);
            if (res.data.token) {
              //   this.$store.commit("user/setusername", res.data);
              this.$message.success("注册成功");
              setTimeout(() => {
                this.$emit("tologin");
              }, 3000);
            }
          });
        } else {
          console.log("注册失败");
        }
      });
    }
  }
};
</script>

<style scoped lang="less">
.form {
  padding: 25px;
}

.form-item {
  margin-bottom: 20px;
}

.form-text {
  font-size: 12px;
  color: #409eff;
  text-align: right;
  line-height: 1;
}

.submit {
  width: 100%;
  margin-top: 10px;
}
</style>